package www.cskaoyan.com.service.impl;

import www.cskaoyan.com.bean.Group;
import www.cskaoyan.com.dao.GroupManagerDao;
import www.cskaoyan.com.service.GroupManagerService;
import www.cskaoyan.com.utils.Response;

import java.sql.SQLException;
import java.util.List;

// ServiceImpl : 逻辑层
public class GroupManagerServiceImpl implements GroupManagerService {

    // 调用持久层
    GroupManagerDao groupManagerDao;
    public GroupManagerServiceImpl(){
        this.groupManagerDao = new GroupManagerDao();
    }

    @Override
    public List<Group> getGroupList() throws SQLException {
        List<Group> list = groupManagerDao.getGroupList();

        return list;
    }


    // 创建分组列表:  根据组名 和 班级 id
    @Override
    public void createGroup(Group group) throws SQLException {
        String groupName = group.getGroupName();
        Integer classId = group.getClassId();

        groupManagerDao.createGroup(groupName, classId);
    }

    // 删除分组
    @Override
    public Response deleteGroup(Group group) throws SQLException {
        Integer groupId = group.getId();
        // TODO: 后续需要确定这个分组下是否有更多同学, 有同学则不删除
//        List<User> list = userManagerDao.selectUserListByGroupID(classId);
//        if (list.size() > 0){
//            return  new Response(10, "请先删除该班级下的分组", false);
//        }

        groupManagerDao.deleteGroup(groupId);

        return new Response(200, "删除成功", true);
    }


    // 修改分组信息
    @Override
    public Response changeGroup(Group group) throws SQLException {
//        parameters={classId=2, groupName=非常帅组, id=1}
        Integer groupId = group.getId();
        Integer classId = group.getClassId();
        String groupName = group.getGroupName();

        groupManagerDao.changeGroup(groupId, classId, groupName);


        return  new Response(200, "修改成功", true);
    }
}
